package io.renren.modules.news.controller;

import io.renren.common.config.FileServerConfig;
import io.renren.common.utils.DfsUtil;
import io.renren.common.utils.FileServerResponse;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.news.entity.TopicEntity;
import io.renren.modules.news.service.ChannelService;
import io.renren.modules.news.service.TopicService;
import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.ContentType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author wxy
 * @email sharonwang_xy@163.com
 * @date 2018-10-15 23:10:19
 */
@RestController
@RequestMapping("news/topic")
public class TopicController {
    @Autowired
    private TopicService topicService;

    @Autowired
    private ChannelService channelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("news:topic:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TopicEntity> topicList = topicService.queryList(query);
        int total = topicService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(topicList, total, query.getLimit(), query.getCurrPage());
//		PageUtils pageUtil = topicService.queryPage(params);

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("news:topic:info")
    public R info(@PathVariable("id") Integer id) {
        TopicEntity topic = topicService.queryObject(id);

        return R.ok().put("topic", topic);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("news:topic:save")
    public R save(@RequestBody TopicEntity topic) {

        topic.setContent(URLDecoder.decode(topic.getContent()));

        //根据公告类型id保存公告类型名
        String channelName = channelService.queryObject(topic.getCid()).getName();
        topic.setCname(channelName);
        topic.setRecommend(0);

        System.out.print("save-----" + topic.getContent());
        topicService.save(topic);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("news:topic:update")
    public R update(@RequestBody TopicEntity topic) {

        topic.setContent(URLDecoder.decode(topic.getContent()));

        //根据公告类型id保存公告类型名
        String channelName = channelService.queryObject(topic.getCid()).getName();
        topic.setCname(channelName);

        System.out.print("update-----" + topic.getContent());
        topicService.update(topic);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("news:topic:delete")
    public R delete(@RequestBody Integer[] ids) {
        topicService.deleteBatch(ids);

        return R.ok();
    }


}
